package com.xhwl.centiotdata.handler.org.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhwl.centiotdata.handler.org.OrgFactory;
import com.xhwl.centiotdata.handler.org.OrgHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.IOrgInfoService;
import com.xhwl.centiotdata.suppliersystem.keytopLocal.OrgKeyTopLocalHandler;
import com.xhwl.common.enums.OrganizationTypeEnum;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.iot.OrgInfo;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author msy
 */
@Service
@Slf4j
public class OrgIntegrationKeyTopLocalHandler extends OrgHandler {

    @Resource
    private OrgKeyTopLocalHandler orgKeyTopLocalHandler;

    @Resource
    private IOrgInfoService iOrgInfoService;

    @Override
    public void afterPropertiesSet() throws Exception {
        OrgFactory.register(ProductEnum.KEYTOP_LOCAL.id, this);
    }


    @Override
    public void createOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        log.info("同步新增组织");
        List<OrgInfo> infoList = new ArrayList<>();
        organizations.forEach(org -> {
            // 如果创建的组织是ORG才能同步
            if (org.getType() == OrganizationTypeEnum.ORG.getId().intValue()) {
                OrgInfo orgInfo = new OrgInfo();
                orgInfo.setOrgName(org.getName());
                orgInfo.setClientId(config.getClientId());
                orgInfo.setParentId(org.getParentId());
                orgInfo.setId(org.getId());
                orgInfo.setEnterpriseId(org.getEnterpriseId());
                orgInfo.setProductId(config.getProductId());
                OrgInfo parentOrgInfo = iOrgInfoService.getOrgInfo(org.getParentId(), config.getProductId());
                if (null != parentOrgInfo && StringUtils.isNotEmpty(parentOrgInfo.getOrgIndexCode())) {
                    orgInfo.setParentIndexCode(parentOrgInfo.getOrgIndexCode());
                } else {
                    orgInfo.setParentIndexCode("0");
                }
                infoList.add(orgInfo);
            }
        });
        List<OrgInfo> updatedInfoList = orgKeyTopLocalHandler.createOrg(config, infoList);
        iOrgInfoService.saveBatch(updatedInfoList);
    }

    @Override
    public void updateOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        organizations.forEach(org -> {
            // 如果创建的组织是ORG才能同步
            if (org.getType() == OrganizationTypeEnum.ORG.getId().intValue()) {
                OrgInfo orgInfo = iOrgInfoService.getOrgInfo(org.getId(), config.getProductId());
                orgInfo.setOrgName(org.getName());
                OrgInfo updateOrg = new OrgInfo();
                BeanUtils.copyProperties(orgInfo, updateOrg);
                boolean updateResult = orgKeyTopLocalHandler.updateOrg(config, updateOrg);
                if (updateResult) {
                    iOrgInfoService.updateOrgInfo(orgInfo);
                }
            }
        });
    }

    @Override
    public void deleteOrg(PedestrianIntegrationConfigDTO config, List<Organization> organizations) {
        // 如果创建的组织是ORG才能同步
        List<Integer> ids = organizations.stream().filter(org -> org.getType() == OrganizationTypeEnum.ORG.getId().intValue())
                .map(Organization::getId).collect(Collectors.toList());
        List<OrgInfo> orgInfos = iOrgInfoService.getList(config.getProductId(), ids);
        if (CollectionUtils.isNotEmpty(orgInfos)) {
            orgInfos.forEach(orgInfo -> {
                orgKeyTopLocalHandler.deleteOrg(config, orgInfo);
            });
            orgInfos.forEach(orgInfo -> iOrgInfoService.deleteOrgInfo(orgInfo.getId(), orgInfo.getProductId()));
        }
    }
}
